class_name RoomDoor
extends Area2D

@export var connect_door:RoomDoor 

var room:Room2D
var src_pos:Vector2
var target_pos:Vector2

func _ready() -> void:
	src_pos = self.global_position
	target_pos = connect_door.global_position
	#set_deferred("monitorable",false)
	#set_deferred("monitoring",false)
	self.body_entered.connect(Callable(self,"_enter_door"))

func _set_up(p_parent_room):
	room = p_parent_room
	room.connections[connect_door] = false

func wake_up():
	set_deferred("monitoring",true)

func sleep():
	set_deferred("monitoring",false)

func _enter_door(body):
	if not is_instance_valid(room):
		return
	if body is MovementEnemy:
		_movement_enemy_enter(body)
	elif body is Player:
		_player_enter(body)

func _movement_enemy_enter(body):
	body = body as MovementEnemy
	var body_is_cross_room = is_instance_valid(body.target_room)
	var target_room_is_same = connect_door.room == body_is_cross_room
	if not body_is_cross_room or not target_room_is_same:
		return
	body.nav_path = [src_pos,target_pos]

func _player_enter(body):
	body = body as Player
	room.wake_up()
